package com.java.gulimall.order.service.impl;

import com.java.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.common.utils.PageUtils;
import com.java.common.utils.Query;

import com.java.gulimall.order.dao.OrderItemDao;
import com.java.gulimall.order.entity.OrderItemEntity;
import com.java.gulimall.order.service.OrderItemService;

/**
 * @RabbitListener: 类+方法
 * @RabbitHandler: 标在方法上 (重载区分不同的消息)
 */
//@RabbitListener(queues = {"hello-queue"})
@Service("orderItemService")
@Slf4j
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     *
     * @param message
     * @param orderEntity 生产者发送的消息实体
     */
    @RabbitListener(queues = {"hello-queue"})
//    @RabbitHandler
    public void receiveMessage(Message message, OrderEntity orderEntity, Channel channel) {
        System.out.println("消息内容:" + message + ", 实体:" + orderEntity);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            // 业务处理成功,手动ACK
            channel.basicAck(deliveryTag,false);
            // 业务处理失败,拒签
//            channel.basicNack(deliveryTag,false,true);
        } catch (Exception e){
            // 网络中断
            log.error("网络中断,{}ack失败",message);
            // 需要防止重复消费
        }
    }
}